package com.lbs.droiddoctor.disease;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;

import com.lbs.droiddoctor.R;
import com.lbs.droiddoctor.base.BaseActivity;
import com.lbs.droiddoctor.common.HttpASync;
import com.lbs.droiddoctor.common.HttpASync.OnRequest;
import com.lbs.droiddoctor.constants.AppConstants;
import com.lbs.view.adapter.SymptomListAdapter;
import com.lbs.view.adapter.SymptomsAdapter;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class AddDisease extends BaseActivity implements OnClickListener ,OnRequest{
	private Button button1;
	private EditText editText1;

	private Button button2;
	private EditText symp;
	private EditText editText4;
	private Button button3;
	private ScrollView scrollView1;
	private ListView symptomsList;
	private SymptomListAdapter symptomsAdapter;
	private ArrayList<String> symptoms;
	private int count = 1;
	private Disease disease;
	private Context ctx;
	private Handler uiHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if (msg.what == 1) {
				SharedPreferences pref = getSharedPreferences(
						AppConstants.PREF_FILE, 0);
				String uName = pref.getString("username", null);
				if (uName.equals("admin")) {
					int position = msg.arg1;
					symptoms.remove(position);
					runOnUiThread(new Runnable() {

						@Override
						public void run() {
							symptomsAdapter.notifyDataSetChanged();

						}
					});

				} else {
					runOnUiThread(new Runnable() {

						@Override
						public void run() {
							Toast.makeText(
									ctx,
									"Your privilages doesn't allows you to delete the disease",
									Toast.LENGTH_LONG).show();

						}
					});
				}
			}

		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.adddisease);
		Bundle bundle = getIntent().getExtras();
		if(bundle!=null){
			disease = bundle.getParcelable("Disease");
		}
		button1 = (Button) findViewById(R.id.submit);
		editText1 = (EditText) findViewById(R.id.editText1);
		symp = (EditText) findViewById(R.id.symptoms_edit);
		button2 = (Button) findViewById(R.id.button2);
		button3 = (Button) findViewById(R.id.button3);
		scrollView1 = (ScrollView) findViewById(R.id.scrollView1);
		symptomsList = (ListView) findViewById(R.id.listView1);
		ctx = this;
		if(disease!=null){
			editText1.setText(disease.getDiseaseName());
			
			symptoms = (ArrayList<String>) disease.getSymptoms();
		}else{
			System.out.println("Disease is null");
			symptoms = new ArrayList<String>();
		}
		symptomsAdapter = new SymptomListAdapter(this, symptoms, uiHandler);
		symptomsList.setAdapter(symptomsAdapter);
		button1.setOnClickListener(this);
		button2.setOnClickListener(this);
		SharedPreferences pref = getSharedPreferences(AppConstants.PREF_FILE,0);
		String uName = pref.getString("username", null);
		if(uName.equals("admin")) {
			button3.setVisibility(View.VISIBLE);
		button3.setOnClickListener(this);
		}
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.submit) {
			String ed1 = editText1.getText().toString();
			// String ed2=editText2.getText().toString();
			if (ed1.equals("")) {
				Toast.makeText(this, "Please enter disease name",
						Toast.LENGTH_SHORT).show();
			}

			String sname = symp.getText().toString();
			if (sname.equals("")) {
				Toast.makeText(this, "Please enter symptom name",
						Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(this,
						"Added symptom: " + sname,
						Toast.LENGTH_SHORT).show();
				symptoms.add(sname);
				symp.setText("");
				symptomsAdapter.notifyDataSetChanged();
			}
			// else if(ed2.equals("")){
			// Toast.makeText(this,"Please enter symptoms ",
			// Toast.LENGTH_SHORT).show();

		} else if (v.getId() == R.id.button3) {
			String ed1 = editText1.getText().toString();
			if (ed1.equals("")) {
				Toast.makeText(this, "Please enter disease name",
						Toast.LENGTH_SHORT).show();
			} else {
				String disease = ed1;
				String  sname = symp.getText().toString();
				String url = AppConstants.ADD_DISEASE_PATH+"&disease="+disease+
						"&sym=";
				int symNo =0;		
				if (!sname.equals("")) {
					url+=sname;
					symNo ++;
				}
				String symname=null; 
				for (int i=0;i<symptoms.size();i++) {
					if(symNo==0) {
						try {
							symname = URLEncoder.encode(symptoms.get(i), "UTF-8");
						} catch (UnsupportedEncodingException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						url+=symname;
						symNo++;
					}else {
						try {
							symname = URLEncoder.encode(symptoms.get(i), "UTF-8");
						} catch (UnsupportedEncodingException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						url+=","+symname;
					}
				}
				System.out.println("URL = "+url);
				
				HttpASync request  = new HttpASync();
				request.setOnRequestListener(this);
				request.execute(url);
				showProgress("Saving disease...");
				
			}

		} else if (v.getId() == R.id.button2) {
			finish();
		}

	}

	@Override
	public void onResponseReceived(String response) {
	    final String res = response;
		runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				hideProgress();
				if(res!=null) {
					Toast.makeText(ctx, res, Toast.LENGTH_LONG).show();
					finish();
				}
				
			}
		});
	}

}